package com.yunshipei.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.enterplorer.R;
import com.yunshipei.ui.activity.DepartmentActivity;
import com.yunshipei.ui.activity.DetailContactsActivity;
import com.yunshipei.model.Departments;
import com.yunshipei.model.ListAndGroup;

import io.rong.app.utils.StringUtilSub;

public class MyListViewAdapter extends BaseAdapter {
    private final int typeMember = 1;
    private final int typeGroup = 2;
    private ListAndGroup listAndGroup;
    private String parentName;
    private LayoutInflater inflater;
    private FragmentActivity activity;
    private OnDepartmentPathForwardListener listener;
    public static int[] person_logos = {R.drawable.person_logo_1, R.drawable.person_logo_2, R.drawable.person_logo_3,
            R.drawable.person_logo_4, R.drawable.person_logo_5, R.drawable.person_logo_6};

    public MyListViewAdapter(FragmentActivity fragmentActivity, String parent) {
        this.activity = fragmentActivity;
        parentName = parent;
        inflater = (LayoutInflater) fragmentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        //人员和部门的总数
        if (listAndGroup == null) {
            return 0;
        }
        //
        int memberSize, departmentSize;
        memberSize = listAndGroup.getMembers().size();
        departmentSize = listAndGroup.getDepartments().size();
        return memberSize + departmentSize;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        if (arg0 < listAndGroup.getMembers().size()) {
            //人员信息
            return listAndGroup.getMembers().get(arg0);
        } else {
            //部门信息
            return listAndGroup.getDepartments().get(arg0 - listAndGroup.getMembers().size());
        }
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < listAndGroup.getMembers().size()) {
            //人员
            return typeMember;
        } else {
            //小组department
            return typeGroup;
        }
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 3;
    }

    @Override
    public View getView(final int position, View view, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ViewHoldermember viewHolder = null;
        ViewHolderGroup vh = null;
        int type = getItemViewType(position);
        if (view == null) {
            switch (type) {
                case typeMember:
                    viewHolder = new ViewHoldermember();
                    view = LayoutInflater.from(activity).inflate(R.layout.item_sort_listview, null);
                    viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);    //字母
                    viewHolder.tvAvatar = (TextView) view.findViewById(R.id.avatarText);    //字母
                    viewHolder.nameTV = (TextView) view.findViewById(R.id.name);        //姓名
                    viewHolder.positionTV = (TextView) view.findViewById(R.id.position);            //职位
                    viewHolder.avatar = (ImageView) view.findViewById(R.id.avatar);            //头像
                    viewHolder.tvLetter.setVisibility(View.GONE);
                    viewHolder.avatar.setImageResource(person_logos[position % person_logos.length]);
                    viewHolder.tvAvatar.setText(SortAdapter.getEndword(listAndGroup.getMembers().get(position).getName()));
                    viewHolder.nameTV.setText(listAndGroup.getMembers().get(position).getName());
                    viewHolder.positionTV.setText(listAndGroup.getMembers().get(position).getPosition());
                    view.setTag(viewHolder);
                    break;
                case typeGroup:
                    int memberLen = listAndGroup.getMembers().size();
                    final Departments department = listAndGroup.getDepartments().get(position - memberLen);
                    view = inflater.inflate(R.layout.common_item, null);
                    vh = new ViewHolderGroup();
                    vh.nameTV = (TextView) view.findViewById(R.id.name);
                    vh.numberTV = (TextView) view.findViewById(R.id.number);
                    vh.nameTV.setText(department.getDepartmentName());
                    vh.numberTV.setText(department.getMemberNumber());
                    view.setTag(vh);
                    break;
                default:
                    break;
            }
        } else {
            if (view.getTag() instanceof ViewHoldermember) {
                viewHolder = (ViewHoldermember) view.getTag();

            } else {
                vh = (ViewHolderGroup) view.getTag();
            }

        }
        switch (type) {
            case typeMember:
                viewHolder.avatar.setImageResource(person_logos[position % person_logos.length]);
                if (StringUtilSub.isChinese(listAndGroup.getMembers().get(position).getName())) {
                    viewHolder.tvAvatar.setText(SortAdapter.getEndword(listAndGroup.getMembers().get(position).getName()));
                } else {
                    viewHolder.tvAvatar.setText(listAndGroup.getMembers().get(position).getName().substring(0, 1).toString().toUpperCase());
                }
                viewHolder.nameTV.setText(listAndGroup.getMembers().get(position).getName());
                viewHolder.positionTV.setText(listAndGroup.getMembers().get(position).getPosition());
                view.setTag(viewHolder);
                String key1 = listAndGroup.getMembers().get(position).getAvatar();
                if (!TextUtils.isEmpty(key1)) {
                    viewHolder.avatar.setTag(listAndGroup.getMembers().get(position).getUuid());
                    viewHolder.tvAvatar.setVisibility(View.GONE);
                } else {
                    viewHolder.tvAvatar.setVisibility(view.VISIBLE);
                }
                if (!TextUtils.isEmpty(key1)) {
//                        BitmapWorkerTask task = new BitmapWorkerTask(viewHolder.avatar);
//                        task.execute(listAndGroup.getMembers().get(position).getAvatar(), listAndGroup.getMembers().get(position).getUuid());
                    Glide.with(activity).load(key1).into(viewHolder.avatar);
                }

                view.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("userInfo", listAndGroup.getMembers().get(position));
                        intent.putExtras(bundle);
                        intent.setClass(activity, DetailContactsActivity.class);
                        activity.startActivity(intent);
                    }
                });
                break;
            case typeGroup:
                int memberLen = listAndGroup.getMembers().size();
                final Departments department = listAndGroup.getDepartments().get(position - memberLen);
                vh.nameTV.setText(department.getDepartmentName());
                vh.numberTV.setText(department.getMemberNumber());
                view.setTag(vh);
                view.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        String departId = department.getDepartmentId();
                        Intent intent = new Intent();
                        intent.putExtra("departId", departId);
                        intent.putExtra("departName", department.getDepartmentName());
                        intent.putExtra("parentDepartName", parentName);
                        if(listener!=null){
                            listener.onDepartmentPathForward(intent);
                        }
                        intent.setClass(activity, DepartmentActivity.class);
                        Log.e("department", "");
                        activity.startActivity(intent);
                    }
                });
                break;
            default:
                break;
        }


        return view;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param listAndGroup
     */
    public void updateListView(ListAndGroup listAndGroup) {
        this.listAndGroup = listAndGroup;
        notifyDataSetChanged();
    }

    class ViewHolderGroup {
        TextView nameTV, numberTV;
        ImageView avatar;
    }

    class ViewHoldermember {
        TextView tvLetter, tvAvatar;//字母
        TextView nameTV, positionTV;
        ImageView avatar;
    }

    public interface OnDepartmentPathForwardListener{
        public void onDepartmentPathForward(Intent intent);
    }

    public void setOnDepartmentPathForwardListener(OnDepartmentPathForwardListener listener){
        this.listener = listener;
    }
}
